$(document).ready(function(){
	$(".totalReply").click(function(){
		var courseId = $('#courseIdHidden').val();
		var commentId = $(this).next().val();
		var parent = $(this).parent().parent().parent();
		$.ajax({
			url : replyUrlGet,
			dataType : 'html',
			data : {courseId : courseId, commentId : commentId},
			success: function(result){
				parent.next().html(result);
				try{
					$(this).val($('#replyNumberUpdate').val());
				}catch(err){
					
				}
			}
		});
	});
	
	$("#postComment").click(function(){
		var courseId = $('#courseIdHidden').val();
		var content = $('#txtPostComment').val();
		if(content != ''){
			$.ajax({
				url : postCommentUrl,
				dataType : 'html',
				type : 'POST',
				data : {courseId : courseId, content : content},
				success: function(result){
					$('#commentList').html(result);
				},
				complete: function(){
					try{
						var errorMessage = $('#errorMessageHidden').val();
						if(!errorMessage){
							$('#txtPostComment').val('');
							$('#errorMessageDisplay').text('');
						}else{
							$('#errorMessageDisplay').text(errorMessage);
						}
					}catch(er){
						$('#txtPostComment').val('');
						$('#errorMessageDisplay').text('');
					}
				}
			});
		}else{
			alert(blankErrorMessage);
			$('#errorMessageDisplay').text(blankErrorMessage);
		}
		
	});
	
	$(document).on("click", ".deleteComment", function(){
		var commentId = $(this).attr('name');
		var courseId = $('#courseIdHidden').val();
		$.ajax({
			url : deleteCommentUrl,
			dataType : 'json',
			type : 'POST',
			data : {data: {courseId : courseId, commentId : commentId}},
			success: function(result){
				if(result.status){
					$('#commentList').html(result.data);
					$('#errorMessageDisplay').html(result.message);
				}else{
					$('#errorMessageDisplay').html(result.message);
				}
			},
			statusCode: {
				403: function() {
					window.location = loginUrl;
				}
			}
		});
	});
	
	$(document).on("click", ".btnPostReply", function(){
		var content = $(this).parent().parent().children('.txtPostReply').val();
		var commentId = $(this).parent().children('.commentHidden').val();
		var courseId = $('#courseIdHidden').val();
		var showAll = $('#showAll'+commentId).val();
		$.ajax({
			url : postReplyUrl,
			dataType : 'json',
			type : 'POST',
			data : {data: {courseId : courseId, commentId : commentId, content : content, showAll : showAll}},
			success: function(result){
				if(result.status){
					$(result.data).insertBefore('#replyPostedContent'+commentId+' .userPosted');
					$('#errorMessageDisplay'+commentId).html('');
					$('#totalReply'+commentId).html(result.total);
					$('#txtPostReply'+commentId).val('');
					
				}else{
					$('#errorMessageDisplay'+commentId).html(result.data);
				}
			},
			statusCode: {
				403: function() {
					window.location = loginUrl;
				}
			}
		});
	});
	
	$(document).on("click", ".deleteReply", function(){
		var commentId = $(this).attr("name");
		var showAll = $('#showAll'+commentId).val();
		$.ajax({
			url : deleteReplyUrl,
			dataType : 'json',
			type : 'POST',
			data : {data :{commentId : commentId, showAll : showAll}},
			success : function(result){
				if(result.status){
					$('#warpReplyPostedContent'+ result.commentId).html(result.data);
					$('#errorMessageDisplay'+commentId).html(result.message);
					$('#totalReply'+result.commentId).html(result.total);
				}else{
					$('#errorMessageDisplay'+commentId).html(result.message);
				}
			}
		})
	});
	
	$(document).on('click', ".deleteCommentLoad", function(){
		var commentId = $(this).attr("name");
		$('#formSubmit'+commentId).submit();
	});
});